import { Frame } from '@/frame';
import UIHeroSkillModel from '../Model/UIHeroSkillModel';
import ObjectPool from '@/ObjectPool';
import SyncUtil from '@/SyncUtil';

export default class UIHeroSkillView {
    pool: ObjectPool<Frame[]> = new ObjectPool(this.poolCreater.bind(this));
    poolCreater() {
        let b = Frame.createGLUETEXTBUTTON();
        b.setParent(this.rootFrame.current).setSize(0.04, 0.04);
        let g = b.addBackgroundImage('');
        b.setOnClick(this.skillButClickHandler.bind(this));
        return [b, g];
    }
    model: UIHeroSkillModel;
    skillButClickHandler() {
        let h = DzGetTriggerUIEventFrame();
        let d = this.frameMapData[h];
        let sk = ClassAbilityMangerInstance.getClassAbilityById(d);
        if (sk != null) {
            this.currentSkillId = d;
            this.tipFrame.setText(`当前选择的技能是：|cffffcc00${sk.abilityName}|r,是要删除这个技能吗？`);
        }
    }
    constructor() {
        this.model = new UIHeroSkillModel();
        // 当玩家选择英雄完毕之后画ui
        se.on('created_hero', this.drawUI.bind(this));
        // 监听技能变化更新ui
        se.on('hero_skill_forget', this.updateUI);
        se.on('hero_skill_level_up', this.updateUI);
    }
    updateUI(this: void, d: { p: player; id: string }) {
        if (d.p != GetLocalPlayer()) return;
        UIManager.UIHeroSkill.showPlayerSkillList(d.p);
    }
    toggleBut: Frame;
    rootFrame: Frame;
    tipFrame: Frame;
    currentSkillId: string = null;
    frameMapData: { [key: number]: string } = {};
    drawUI() {
        let bg = Frame.createBackDrop();
        bg.setSize(0.03, 0.03).setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.08, 0.165).setTexture('assets\\forgot.blp');
        let but = (this.toggleBut = Frame.createGLUEBUTTON());
        but.setSize(0.03, 0.03).setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.08, 0.165);
        but.setOnClick(this.showSkillList.bind(this));

        let root = (this.rootFrame = Frame.createFrame());
        root.setSize(0.16, 0.18).setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.32, 0.2);
        let rbg = new Frame('BACKDROP', null, root.current, '_sl_border_backdrop', 0);
        rbg.setPoints(root.current, 0.007, 0.007);

        let title = Frame.createTEXT();
        title.setParent(root.current).setSize(0.1, 0.015).setPoint(FRAMEPOINT_TOP, root.current, FRAMEPOINT_TOP).setText('删除技能').setTextAlignment(TEXTALIGN_CENTER);
        let tip = (this.tipFrame = Frame.createTEXT().setFont(0.01113, 'fonts\\fonts.ttf'));
        tip.setParent(root.current).setSize(0.16, 0.023).setPoint(FRAMEPOINT_BOTTOM, root.current, FRAMEPOINT_BOTTOM, 0, 0.03).setText('请选择要删除的技能').setTextAlignment(TEXTALIGN_CENTER);

        let conf = Frame.createGLUETEXTBUTTON();
        conf.setParent(root.current).setSize(0.04, 0.02).setPoint(FRAMEPOINT_BOTTOM, root.current, FRAMEPOINT_BOTTOM, 0, 0.005);
        conf.addBackgroundImage('assets\\common\\but_del.blp');
        conf.setOnClick(this.removeSkill.bind(this));

        let but_c = Frame.createGLUEBUTTON();
        but_c.setParent(root.current).setSize(0.015, 0.015).setPoint(FRAMEPOINT_TOPRIGHT, root.current, FRAMEPOINT_TOPRIGHT, -0.005, -0.005).addBackgroundImage('assets\\common\\but_close.blp');
        but_c.setOnClick(this.toggleUI.bind(this));

        root.visible = false;
    }
    toggleUI() {
        this.rootFrame.visible = !this.rootFrame.visible;
        if (this.rootFrame.visible) {
            this.tipFrame.setText(`请选择要删除的技能`);
            this.currentSkillId = null;
        }
    }
    showSkillList() {
        let p = DzGetTriggerUIEventPlayer();
        this.toggleUI();
        this.showPlayerSkillList(p);
    }
    removeSkill() {
        let p = DzGetTriggerUIEventPlayer();
        SyncUtil.syncObjData('hero_forget_skill', { p, id: this.currentSkillId });
    }
    showPlayerSkillList(p: player) {
        if (p != GetLocalPlayer()) return;
        let d = this.model.DB.get(p);
        if (d == null) return;

        this.pool.setAllIdleStatus(true);
        let sks = this.getLearnedAbilities(p);
        this.frameMapData = {};
        for (let i = 0; i < 6; i++) {
            let [b, g] = this.pool.borrowObject();
            let x = 0.015 + 0.045 * (i % 3);
            let y = -0.025 - 0.045 * Math.floor(i / 3);
            b.setPoint(FRAMEPOINT_TOPLEFT, this.rootFrame.current, FRAMEPOINT_TOPLEFT, x, y);
            if (sks[i]) {
                g.setTexture(sks[i].art);
                this.frameMapData[b.current] = sks[i].id;
            } else {
                g.setTexture('assets\\bg_64x64.blp');
            }
        }

        this.pool.forDirtyObjects((f) => {
            f[0].visible = false;
        });
    }
    getLearnedAbilities(p: player) {
        return this.model.DB.get(p).skillIds.map((id) => ClassAbilityMangerInstance.getClassAbilityById(id));
    }
}
